package com.yyy.dm.consumer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yyy.dm.CommonResponse;
import com.yyy.dm.VoUtil;
import com.yyy.dm.dao.LinkUserMapper;
import com.yyy.dm.user.DmLinkUser;
import com.yyy.dm.user.TicketBuyerDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class LinkUserConsumerService {

    @Resource
    private LinkUserMapper linkUserMapper;

    public CommonResponse ticketBuyerList(Long userId){
        List<TicketBuyerDTO> ticketBuyerDTOList = linkUserMapper.findTicketBuyerByUserId(userId);
        return VoUtil.returnSuccess(ticketBuyerDTOList);
    }

    public CommonResponse validateBuyerExist(String idCard){
        QueryWrapper<DmLinkUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("idCard",idCard);
        Integer count = linkUserMapper.selectCount(queryWrapper);
        if (count==0){
            return VoUtil.returnSuccess(count);
        }
        return VoUtil.returnFail("1009","用户已存在");
    }

    public CommonResponse addTicketBuyer(Map<String,String> map){
        DmLinkUser dmLinkUser = new ObjectMapper().convertValue(map,DmLinkUser.class);
        dmLinkUser.setCreatedTime(LocalDateTime.now());
        dmLinkUser.setUpdatedTime( LocalDateTime.now());
        linkUserMapper.insert(dmLinkUser);
        return VoUtil.returnSuccess(null);
    }

    public CommonResponse deleteTicketBuyer(Long linkId){
        linkUserMapper.deleteById(linkId);
        return VoUtil.returnSuccess(null);
    }
}
